package org.gap.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.gap.domain.Naf;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Naf entities.
 * 
 */
@Repository("NafDAO")
@Transactional
public class NafDAOImpl extends AbstractJpaDao<Naf> implements NafDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Naf.class }));

	/**
	 * EntityManager injected by Spring for persistence unit 
	 *
	 */
	@PersistenceContext(unitName = "")
	private EntityManager entityManager;

	/**
	 * Instantiates a new NafDAOImpl
	 *
	 */
	public NafDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findNafByNafLibelle
	 *
	 */
	@Transactional
	public Set<Naf> findNafByNafLibelle(String nafLibelle) throws DataAccessException {

		return findNafByNafLibelle(nafLibelle, -1, -1);
	}

	/**
	 * JPQL Query - findNafByNafLibelle
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Naf> findNafByNafLibelle(String nafLibelle, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNafByNafLibelle", startResult, maxRows, nafLibelle);
		return new LinkedHashSet<Naf>(query.getResultList());
	}

	/**
	 * JPQL Query - findNafByPrimaryKey
	 *
	 */
	@Transactional
	public Naf findNafByPrimaryKey(String nafNaf) throws DataAccessException {

		return findNafByPrimaryKey(nafNaf, -1, -1);
	}

	/**
	 * JPQL Query - findNafByPrimaryKey
	 *
	 */

	@Transactional
	public Naf findNafByPrimaryKey(String nafNaf, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findNafByPrimaryKey", nafNaf);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findNafByNafNaf
	 *
	 */
	@Transactional
	public Naf findNafByNafNaf(String nafNaf) throws DataAccessException {

		return findNafByNafNaf(nafNaf, -1, -1);
	}

	/**
	 * JPQL Query - findNafByNafNaf
	 *
	 */

	@Transactional
	public Naf findNafByNafNaf(String nafNaf, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findNafByNafNaf", nafNaf);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAllNafs
	 *
	 */
	@Transactional
	public Set<Naf> findAllNafs() throws DataAccessException {

		return findAllNafs(-1, -1);
	}

	/**
	 * JPQL Query - findAllNafs
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Naf> findAllNafs(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllNafs", startResult, maxRows);
		return new LinkedHashSet<Naf>(query.getResultList());
	}

	/**
	 * JPQL Query - findNafByNafNafContaining
	 *
	 */
	@Transactional
	public Set<Naf> findNafByNafNafContaining(String nafNaf) throws DataAccessException {

		return findNafByNafNafContaining(nafNaf, -1, -1);
	}

	/**
	 * JPQL Query - findNafByNafNafContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Naf> findNafByNafNafContaining(String nafNaf, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNafByNafNafContaining", startResult, maxRows, nafNaf);
		return new LinkedHashSet<Naf>(query.getResultList());
	}

	/**
	 * JPQL Query - findNafByNafLibelleContaining
	 *
	 */
	@Transactional
	public Set<Naf> findNafByNafLibelleContaining(String nafLibelle) throws DataAccessException {

		return findNafByNafLibelleContaining(nafLibelle, -1, -1);
	}

	/**
	 * JPQL Query - findNafByNafLibelleContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Naf> findNafByNafLibelleContaining(String nafLibelle, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNafByNafLibelleContaining", startResult, maxRows, nafLibelle);
		return new LinkedHashSet<Naf>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Naf entity) {
		return true;
	}
}
